/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
    let sum = 0, carry = 0;
    let newList = null, current = null;
    let getData = (list) => { return list ? list.val : 0 };
    let getPointer = (list) => { return list ? list.next : list };
    while (l1 || l2) {
        sum = getData(l1) + getData(l2) + carry;
        if (sum >= 10) { 
            sum = sum - 10;
             carry = 1; 
        }else{
            carry=0;
        }
        if (!current) {
            newList = new ListNode(sum);
            current =newList;
        } else {
            current.next = new ListNode(sum);
            current = current.next;
        }

        l1 = getPointer(l1);
        l2 = getPointer(l2);

    }
    if(carry>=1){
        current.next=new ListNode(carry)
    }

    return newList;
};